easiest way to sort?

Hi,
I am new to DXL, but not to programmming. However, I still like to see better examples than what is in the documentation.

I am building a userlist into a buffer and then displaying the buffer using a print command.
I actually inherited the script from another person and I am tweaking it.

What I would like to is have the userList either sorted before hand, or sort what I will be outputting.
I am not sure what the best way would be since I am so new to DXL.
I woudl love to see the IBM scripts they use when you see the users in the message box. But, they are encrypted and I
do not know how to decrypt, or even if they can be decrypted.

So, just for grins here is the script:
Note: this script is really almost a playground type script. It looks like the person was llooking at several things
in trying to do something.

User user
Buffer b = create
b= ""
// *** possible to sort here??
for user in userList do {
bool c = user.disabled // do not display disabled users
if (!c){
string sysusr = user.systemLoginName
if (length (string sysusr) > 7 || (null sysusr)) // Check if System Login Name valid
{
string sysusr = "* Invalid SYS Usr*"
}
if (null sysusr) sysusr = "* BLANK SYS User *"
string fname = user.fullName
if (null fname) fname = "* Blank Full Name *"
string usrname = user.name
string addr = user.address
if (null addr) addr=" * Blank Addr * "
string sso = user.description
if (null sso) sso = " * Blank SSO * "
int n = firstNonSpace(Buffer b)
string fnosp = n""
i = length sso
string nosp = i ""
ssos = sso0:i-1

string tele = user.telephone
if (null tele) tele = " * Blank Tele *"
string email = user.email
if (null email) email = "* Blank Email *"
b+= fname";"email";"sysusr";"usrname";"tele";"addr ";"sso";"nosp";"ssos"; first no space"fnosp";""\n"
}
}
// **** Can the buffer b be sorted?? ****
print b "\n"
ack "Done."

Any ideas on what would be the best method to sort, or even if it is possible?
I hope somebody can help.
Thanks.
Jerry
SystemAdmin - Wed Jul 13 15:35:58 EDT 2011

Re: easiest way to sort?
OurGuest - Wed Jul 13 16:19:05 EDT 2011

Look at skiplist.

Re: easiest way to sort?
OurGuest - Thu Jul 14 08:24:26 EDT 2011

Here is an example using skiplist to sort:
 

Skip skpList=createString
put(skpList,"d","dog")
put(skpList,"c","cat")
put(skpList,"b","boy")
put(skpList,"a","alligator")
string s
for s in skpList do print s "\n"

 


Output is:
alligator
boy
cat
dog

 

Re: easiest way to sort?
llandale - Thu Jul 14 11:34:01 EDT 2011

OurGuest - Thu Jul 14 08:24:26 EDT 2011

Here is an example using skiplist to sort:
 

Skip skpList=createString
put(skpList,"d","dog")
put(skpList,"c","cat")
put(skpList,"b","boy")
put(skpList,"a","alligator")
string s
for s in skpList do print s "\n"

 


Output is:
alligator
boy
cat
dog

 

Little more clarification: retrievals are sorted lowest first for KEY string and integer and I suppose real and char; other data types come out non-sensically, what does it mean to sort a 'Link' handle? (OK, they come out in the order in which they are stored in memory).

Skip skpList=createString()  // 'createString' ALWAYS when KEY is type 'string'; otherwise 'create'
put(skpList,"d","dog")
put(skpList,"c","cat")
put(skpList,"b","boy")
put(skpList,"b","boyX")      // This one ignored since key 'b' already exists
put(skpList,"a","alligator")
string Data, Key
for Data in skpList do         // Notice here we have to remember what 'type' is the Data
{  Key = (string key skpList)  // Notice here we have to remember what 'type' is the Key
   print Key "\t" Data "\n"
}
delete(skpList)


You can force retrieval in natural order as follows:

 

Skip skpLinks = create()   // KEY 'int' sequencer, DATA: 'Link'
Link lnk
Object o = current
int  Sequencer = 0
for lnk in o ->"*" do
{  put(skpLinks, Sequencer++, lnk)
}
for lnk in skpLinks do
{  Sequencer = (int key skpLinks)
   print Sequencer "\t" some info about the link "\n"
}
delete(skpLinks)


If the handle has a name, such as 'Module', you could but the name as key and data and retrieve alphabetically.

It may seem silly to stage them in a Skip list, but not ..
<1> if you figure to disrupt the natural loop by deleting or creating handles (Link in this case)
<2> there may be the same loop inside some function inside this loop
<3> there are strange cases where exiting the loop with 'return' or 'break' mess things up.

It should be routine to stage all these odd handles in a skip list first as shown, even if you cannot think of a good reason to do so.

 

 

 

  • Louie

 

 

Re: easiest way to sort?
llandale - Thu Jul 14 11:58:32 EDT 2011

You cannot "sort" a buffer but you can sort the Users before you parse them and put their info into the Buffer.

Change these two lines...
for user in userList do {
   bool c = user.disabled // do not display disabled users
...
To these lines...
 
Skip skpUsers = createString()  // KEY 'string' UserName; DATA: 'User' handle
string Name
for user in userList do {
   Name = user.name
   put(skpUsers, Name, user)
}
for user in skpUsers do {
   bool c = user.disabled // do not display disabled users
 
... and of course add "delete(skpUsers)" after the loop.

 

 

  • Louie


Nit-Picks:
> When posting, surround your code with "{co de}" without the quotes or the space. Formats the code as you typed it. Practice with "preview".
> Its a lot easier to read code when you declare all variables up front. It can also be ineffecient to declare them inside loops, for time and for wasted memory.
> Line 11: string sysusr = "* Invalid SYS Usr*". Get rid of the "string" since you want variable "sysusr" to be the same one as outside the loop.
> Line 21: int n = firstNonSpace(Buffer b). Get rid of "Buffer". Probably doesn't matter but its confusing.
> Don't follow taht first non-space stuff, but I'm pretty sure you don't want the first non-space from the buffer, but rather from "sso".
> I think "print stringOf(b)" is less memory wasteful. That doesn't matter for this tiny script but does of huge scripts parsing 100s of modules.
> delete(b) at bottom.
> Using a variable "user" the same spelling as native commands gives me the HeeBeeGeeBees; since there are times where it fails; and when it fails its practically impossible to find. Get in the habit of avoiding that, "User usr" is better.

 

 

testing
SystemAdmin - Thu Jul 21 12:17:00 EDT 2011

testing
// Remove newline f
 
Buffer bTempStrip = create() // create the temp buffer outside of the function for performance
void StripNewLine(Buffer b)
{ 
  setempty bTempStrip ; // empty the temp buffer
  int j,l=length(b)
  if (l == 0) return // nothing to do
  b2 = ""
  for(j=0;j<l;j++) { if(b[j] != '\n') bTempStrip += b[j] }
  setempty b; combine (b, bTempStrip, 0)  // empty the old buffer, and replace its contents
}
 
Buffer b1 =create
Buffer b1Alias = b1
b1= "First Line\nSecond Line"
StripNewLine(b1)
print (tempStringOf b1) ""
print "Hey our alias still works: " (tempStringOf b1Alias) ""
delete(b1)